<?php
if (!$auth->getReadAccess(75));
//require_once($__CFG_dir_admincontrol . "globalclassdata.php");
$gb_sub = new GB($__CFG_http_gb_sub);

$sql = "SELECT mt_menu.id_menu, mi.menu as menu_induk, mt_menu.menu from mt_menu
    LEFT JOIN mt_menu mi ON mi.id_menu = mt_menu.id_menu_induk";
$cont = new GCD($sql);

$sql1 = "SELECT * from hak_akses ";
$cont1 = new GCD($sql1);




require_once($__CFG_dir_lib . "filterpanel.php");
$fp = new FilterPanel($url, $_fldname, $_fldtampil, $_fldtype);
$_fldname = array("mt_menu.menu", "mi.menu");
$_fldtampil = array("Nama Menu", "Kelompok Menu");
$_fldtype = array("text", "text");

$whr = " mt_menu.id_menu_induk IS NOT NULL ";
//$whr = $fp->getFilterString();
//$whr = $whr == "" ? " id_menu='" . $kode . "'": $whr;

$action = $_REQUEST["act"];

if($action=="rem" && $kode!="") {

        $cont->deleteData("mt_menu", "id_menu", $kode);

        $arrValsLog = array(date("Y-m-d H:i:s"), $auth->getUserID(), "Menghapus Data Pengguna dengan ID Pengguna ". $kode);  //"log_time", "log_aktor", "log_event"
        $cont->insertLog($arrValsLog);
}else if($action=="read"){
                $_staktif = $_GET["status"]=="0"?"1":"0";
                $_id_menu = $_GET["id_menu"] == ""? 0: $_GET["id_menu"];
                $_info = $_GET["status"]=="0"?"Mengaktifkan":"Menonaktifkan";
                if($cont1->checkDuplicateKeyByConds(" id_menu=". $_id_menu ." AND id_user='". $kode ."'")){
                    $cont1->updateData("hak_akses", array("is_read"), array($_staktif), "id_user='$kode' AND id_menu='$_id_menu'");
                }else{
                    $arrFields = array("id_user", "id_menu", "is_read");
                    $arrVals = array($kode, $_id_menu, $_staktif);
                    $cont1->insertData("hak_akses", $arrFields, $arrVals);
                    
                }
                               
                $arrValsLog = array(date("Y-m-d H:i:s"), $auth->getUserID(), $_info." akses read pada Id User ". $kode);  //"log_time", "log_aktor", "log_event"
                $cont->insertLog($arrValsLog);



}
else if($action=="modify"){
                $_staktif = $_GET["status"]=="0"?"1":"0";
                $_id_menu = $_GET["id_menu"];
                $_info = $_GET["status"]=="0"?"Mengaktifkan":"Menonaktifkan";
                if($cont1->checkDuplicateKeyByConds(" id_menu=". $_id_menu ." AND id_user='". $kode ."'")){
                    $cont1->updateData("hak_akses", array("is_modify"), array($_staktif), "id_user='$kode' AND id_menu='$_id_menu'");
                }else{
                    $arrFields = array("id_user", "id_menu", "is_modify");
                    $arrVals = array($kode, $_id_menu, $_staktif);
                    $cont1->insertData("hak_akses", $arrFields, $arrVals);
                    
                }
                $cont->updateData("hak_akses", array("is_modify"), array($_staktif), "id_user='$kode' AND id_menu='$_id_menu'");
                $arrValsLog = array(date("Y-m-d H:i:s"), $auth->getUserID(), $_info." akses modify pada Id User ". $kode);  //"log_time", "log_aktor", "log_event"
                $cont->insertLog($arrValsLog);

}
else if($action=="delete"){
                $_staktif = $_GET["status"]=="0"?"1":"0";
                $_id_menu = $_GET["id_menu"];
                $_info = $_GET["status"]=="0"?"Mengaktifkan":"Menonaktifkan";
                if($cont1->checkDuplicateKeyByConds(" id_menu=". $_id_menu ." AND id_user='". $kode ."'")){
                    $cont1->updateData("hak_akses", array("is_delete"), array($_staktif), "id_user='$kode' AND id_menu='$_id_menu'");
                }else{
                    $arrFields = array("id_user", "id_menu", "is_delete");
                    $arrVals = array($kode, $_id_menu, $_staktif);
                    $cont1->insertData("hak_akses", $arrFields, $arrVals);
                    
                }
                $cont->updateData("hak_akses", array("is_delete"), array($_staktif), "id_user='$kode' AND id_menu='$_id_menu'");
                $arrValsLog = array(date("Y-m-d H:i:s"), $auth->getUserID(), $_info." akses delete pada Id User ". $kode);  //"log_time", "log_aktor", "log_event"
                $cont->insertLog($arrValsLog);

}elseif($action=="exp"){

        $QRYEXPORT = "SELECT 
                        mt_user.id_user AS ID_PENGGUNA,
                        mt_karyawan.nama AS NAMA
                        FROM mt_user
                        LEFT JOIN mt_karyawan ON mt_karyawan.nik = mt_user.nik ";

        $QRYEXPORT = $QRYEXPORT . ($whr != ""? " where ". $whr : "");

        $arrValsLog = array(date("Y-m-d H:i:s"), $auth->getUserID(), "Export Data Pengguna ");  //"log_time", "log_aktor", "log_event"
        $cont->insertLog($arrValsLog);

        $filename = $__CFG_dir_export . $auth->getUserID() .".csv";

        $cmd = "touch $filename";
        shell_exec($cmd);

        $cmd = "chmod 0777 $filename";
        shell_exec($cmd);

        $cmd = "psql -U". $__CFG_dbuser ." ". $__CFG_dbname ." -c  \"COPY (". $QRYEXPORT .") TO '". $filename ."' WITH CSV HEADER\"";
        shell_exec($cmd);

        echo "<script>window.open('". $__CFG_http_export . $auth->getUserID() .".csv', 'wndStaff', 'width=730,height=500,directories=0,location=0,resizable=1,scrollbars=1,toolbar=0,copyhistory=0,status=0')</script>";

}

$offset = $_REQUEST["os"];
$nextPage = $_REQUEST["np"];
$colPage = $_REQUEST["cp"];
if($offset == "") {
	$offset = 0;
}
if($nextPage == "") {
	$nextPage = 1;
}
if($colPage == "") {
	$colPage = 1;
}


$cont->getResultSet($whr, " mt_menu.id_menu_induk, mt_menu.urutan ASC ", $sc_pageslimit, $offset);


$cont1->getResultSet(" id_user='$kode'", "", 0, 0);


$arrHakAkses = array ();
if($cont1->countListData() > 0) {
	
	foreach($cont1->listData() as $rowAccess) {
                $arrHakAkses[$rowAccess->id_menu]["is_read"] = $rowAccess->is_read;
                $arrHakAkses[$rowAccess->id_menu]["is_modify"] = $rowAccess->is_modify;
                $arrHakAkses[$rowAccess->id_menu]["is_delete"] = $rowAccess->is_delete;
        }
}
setPageTitle("Hak Akses Pengguna");
//$add_header_items = "\n<script type=\"text/javascript\" src=\"". $__CFG_http_js ."jquery.js\"></scr"."ipt>\n<script type=\"text/javascript\" src=\"". $__CFG_http_js ."jquery.validate.js\"></scr"."ipt>\n<script type=\"text/javascript\" src=\"". $__CFG_http_js ."validate_staff_child.js\"></scr"."ipt>";
require_once($__CFG_dir_adminlayout . $__CFG_site_adminlayout."/header_detil.php");

?>
<script language="javascript" type="text/javascript">
	
	function getValueSingle(kode, nama, elm) {
		try {
			parent.parent._obj_fSendValue(kode, nama, 'id_biller', 'biller');
		}
		catch(e) { alert(e.message); }
		parent.parent.focus();

		<?php echo CloseGB(false); // close grebox?>
                 
	}
        
        function setIsActive(kode, id_menu, action, flag) {
	if(confirm('Anda yakin akan mengubah hak akses user '+ kode +' ini ?')) {
		location.href = '<?php echo $__CFG_http_adminroot; ?>index.php/popup/hak_akses/' + kode + '/?act=' + action + '&id_menu='+ id_menu +'&status='+ flag;
	}
}
</script>

<div id="content">
<h3 class="title-popup">Hak Akses Pengguna</h3>



<div class="DivTable">
    <div class="box-control">
<?php

//echo "<a href=\"#\" onclick=\"javascript:exportData()\" class=\"report\" title=\"Export Data\">&nbsp;</a>";
//echo "<a href=\"#\" class=\"search\" title=\"Cari Data\">&nbsp;</a>";

?>
</div>
<table class="GridTable">

<!--  ************   begin form search ****************** -->
<tr class="form-search">
<td colspan="8">
<?php echo $fp->drawfilterpanel("", $_fldname, $_fldtampil, $_fldtype)?>
</td>
</tr>
<!-- ************   end form search ********************* -->

<tr class="GridHeader">
<td>No.</td>
<td>ID Menu</td>
<td>Nama Menu</td>
<td>Kelompok Menu</td>
<td>Set Read</td>
<td>Set Modify</td>
<td>Set Delete</td>
</tr>
<?php

if($cont->countListData() > 0) {
	$nomor = $offset;
	foreach($cont->listData() as $row) {
		$nomor++;
		
//                $kodecontent = $row->id_user;
                
		$styleTR = "GridItem1";
                
?>
		<tr class="<?php echo $styleTR;?>">
		<td><?echo $nomor;?></td>
                <td><?echo $row->id_menu;?></td>
                <td><?echo $row->menu;?></td>
                <td><?echo $row->menu_induk;?></td>
                <td >
                        <?php
                            if($arrHakAkses[$row->id_menu]["is_read"]){
                                echo "<a href=\"#\" class=\"setaktif\" title=\"Set Non Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'read', '1')\">&nbsp;</a>";
                            }else{
                                echo "<a href=\"#\" class=\"setnonaktif\" title=\"Set Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'read', '0')\">&nbsp;</a>";
                            }

                        ?>
                </td>
                <td >
                        <?php
                            if($arrHakAkses[$row->id_menu]["is_modify"]){
                                echo "<a href=\"#\" class=\"setaktif\" title=\"Set Non Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'modify', '1')\">&nbsp;</a>";
                            }else{
                                echo "<a href=\"#\" class=\"setnonaktif\" title=\"Set Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'modify', '0')\">&nbsp;</a>";
                            }

                        ?>
                </td>
                <td >
                        <?php
                            if($arrHakAkses[$row->id_menu]["is_delete"]){
                                echo "<a href=\"#\" class=\"setaktif\" title=\"Set Non Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'delete', '1')\">&nbsp;</a>";
                            }else{
                                echo "<a href=\"#\" class=\"setnonaktif\" title=\"Set Aktif\" onclick=\"javascript:setIsActive('". $kode ."', '" . $row->id_menu . "', 'delete', '0')\">&nbsp;</a>";
                            }

                        ?>
                </td>
		</tr>
                
<?php
	}

}
else {
?>
	<tr class="GridItem1">
		<td colspan="5">Tidak ada data</td>
	</tr>

<?php
}
?>
</table>
</div>
<?php echo $pg->createPage($cont->__returnRows, $offset, $sc_pageslimit, $nextPage, 10, $colPage);?>
<br class="bersih" />
<script type="text/javascript">

function exportData(){
	location.href = '<?php echo $__CFG_http_adminroot; ?>index.php/popup/hak_akses/<?php echo $kode;?>/?act=exp&<?php echo $_SERVER["QUERY_STRING"]?>';
}

jQuery(document).ready(function(){
		jQuery(".form-search").hide();

		jQuery('.search').click(
			function(){
			jQuery(".form-search").slideToggle();
			}
			);

		});

</script>